package cn.cool.basic;

import cn.cool.config.CdConfigSetting;
import cn.cool.config.CdDefaultSetting;
import cn.cool.exception.CdBasicException;
import cn.cool.util.CdConstant;
import cn.cool.util.CdUtil;

import java.io.File;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Objects;
import java.util.Properties;

public class CdResource {

    public static final String CONST_SYMBOL_DOT = ".";
    public static final String CONST_SUFFIX_PROPERTIES = ".properties";

    public Properties readResource(CdConfigSetting configSet) {
        getCdDefaultSetting(configSet);
        if (CdDefaultSetting.IS_PRINT_BANNER) CdUtil.printCdDropdownInfo();

        String fileName = getFileName(CdDefaultSetting.RESOURCE_FILE_NAME);
        File file = new File(fileName);
        if (!file.exists()) {
            throw new CdBasicException(CdConstant.GL_CODE_FILE_NOT_EXIST_KEY, CdDefaultSetting.RESOURCE_FILE_NAME + "文件不存在");
        }

        Properties properties = new CdProperties();
        try {
            InputStreamReader fileReader = new InputStreamReader(Files.newInputStream(file.toPath()), StandardCharsets.UTF_8);
            properties.load(fileReader);
            fileReader.close();
        } catch (Exception e) {
            throw new CdBasicException(CdConstant.GL_CODE_FILE_NOT_EXIST_KEY, CdDefaultSetting.RESOURCE_FILE_NAME + "文件不存在");
        }
        return properties;
    }

    private String getResourceFilePath() {
        String path = "";
        try {
            path = Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("")).getPath();
        } catch (NullPointerException e) {
            throw new CdBasicException(CdConstant.GL_CODE_FILE_PATH_ERROR_KEY, "文件路径获取异常");
        }
        return path;
    }

    private String getFileName(String propertyFileName) {
        if (CdUtil.isStrEmpty(propertyFileName)) {
            return getResourceFilePath() + CdDefaultSetting.RESOURCE_FILE_NAME + CONST_SUFFIX_PROPERTIES;
        }

        String strTmp = propertyFileName.replace(CONST_SYMBOL_DOT, "");
        if (propertyFileName.length() - strTmp.length() > 1) {
            throw new CdBasicException(CdConstant.GL_CODE_FILE_NAME_ERROR_KEY, "文件名称不合法");
        } else if (propertyFileName.length() - strTmp.length() == 1 && propertyFileName.endsWith(CONST_SUFFIX_PROPERTIES)) {
            return getResourceFilePath() + propertyFileName;
        }
        return getResourceFilePath() + propertyFileName + CONST_SUFFIX_PROPERTIES;
    }

    private void getCdDefaultSetting(CdConfigSetting configSet) {
        if(null == configSet) return;
        if(null != configSet.getPrintBanner()) {
            CdDefaultSetting.IS_PRINT_BANNER = configSet.getPrintBanner();
        }
        if(null != configSet.getResourceFileName()) {
            CdDefaultSetting.RESOURCE_FILE_NAME = configSet.getResourceFileName();
        }
        if(null != configSet.getDropdownTopEmptyKey()) {
            CdDefaultSetting.DROPDOWN_TOP_EMPTY_KEY = configSet.getDropdownTopEmptyKey();
        }
        if(null != configSet.getDropdownTopEmptyValue()) {
            CdDefaultSetting.DROPDOWN_TOP_EMPTY_VALUE = configSet.getDropdownTopEmptyValue();
        }
        if(null != configSet.getDropdownTopEmptyComment()) {
            CdDefaultSetting.DROPDOWN_TOP_EMPTY_COMMENT = configSet.getDropdownTopEmptyComment();
        }
    }

}
